Questions
29 of 30
1What are functions and operators in MySQL, and how are they different?
2What is the difference between single-row functions and aggregate functions?
3Give examples of commonly used string functions in MySQL.
4What is the use of the CONCAT() function? How is it different from using ||?
5Explain the difference between NOW(), CURDATE(), and SYSDATE().
6What are arithmetic operators in MySQL? Give examples.
7What is the difference between the = and <=> operators?
8What does the DISTINCT keyword do when used with aggregate functions like COUNT()?
9What is the difference between IFNULL() and COALESCE() functions?
10What are logical operators in MySQL, and how do AND, OR, and NOT work?
11What is the difference between LENGTH() and CHAR_LENGTH() functions?
12How does MySQL handle type conversion when using operators on different data types?
13What is the difference between ROUND(), TRUNCATE(), and FORMAT() functions?
14Explain how CASE and IF() functions can be used for conditional logic.
15What are comparison operators in MySQL, and how are they used with NULL values?
16How do aggregate functions like AVG(), SUM(), and MAX() behave when NULLs are present?
17Explain the use of REGEXP and LIKE operators. What’s the difference between them?
18What are user-defined functions (UDFs), and how do they differ from stored procedures?
19What are the differences between DATE_ADD() and ADDDATE() functions?
20How can you use STR_TO_DATE() and DATE_FORMAT() for converting and displaying date values?
21How does MySQL internally optimize and cache results of deterministic functions?
22What are window functions in MySQL 8.0, and how do they differ from aggregate functions?
23Explain the difference between RANK(), DENSE_RANK(), and ROW_NUMBER() window functions.
24How does MySQL evaluate operator precedence when multiple operators are used in a single expression?
25Can functions be used in the WHERE clause? What are the performance implications?
26How can you use JSON_EXTRACT() and JSON_CONTAINS() to work with JSON data in MySQL?
27What are the performance trade-offs of using scalar functions inside JOIN or GROUP BY clauses?
28Explain how collation affects comparison operators in string functions.
29What are deterministic and non-deterministic functions? How does this affect replication and indexes?
30How would you combine multiple functions and operators to clean, transform, and aggregate data efficiently in one query?
29 / 30

What are deterministic and non-deterministic functions? How does this affect replication and indexes?

Deterministic vs Non-Deterministic Functions in MySQL

In MySQL, functions can be classified as deterministic or non-deterministic based on whether they always return the same result for the same input values.

1. Deterministic Functions
  1. 1

    Always return the same result for the same input.

  2. 2

    Examples: ABS(x), UPPER(str), CONCAT(str1, str2), custom UDFs declared DETERMINISTIC.

  3. 3

    Can be safely used in indexes, generated columns, and stored function optimizations.

  4. 4

    Replication-safe: their results are predictable across master and replica servers.

2. Non-Deterministic Functions
  1. 1

    May return different results for the same input.

  2. 2

    Examples: NOW(), RAND(), UUID(), CURRENT_DATE(), custom UDFs not declared DETERMINISTIC.

  3. 3

    Cannot be reliably used in indexed generated columns, as results may vary.

  4. 4

    Replication caution: non-deterministic functions can cause inconsistencies between master and replicas if evaluated differently.

3. Implications for Indexes and Generated Columns
  1. 1

    Deterministic functions allow creating indexes on expressions or generated columns because the output is stable.

  2. 2

    Non-deterministic functions cannot be used in indexed expressions, as the index would not reflect consistent values.

  3. 3

    Using non-deterministic functions in WHERE clauses or JOINs is fine but may prevent optimization or caching.

In summary: Deterministic functions are predictable and can be optimized, indexed, and safely replicated. Non-deterministic functions may return varying results, cannot be used in indexed generated columns, and require care in replication to ensure consistency across servers.